/**
 * FileName: EchoServer
 * Author:   powersi
 * Date:     2021/3/1 17:48
 * Description:
 */
package com.demo.zm.netty.sample.echo;

import com.demo.zm.netty.sample.echo.codec.ClientLengthDecoder;
import com.demo.zm.netty.sample.echo.codec.ClientLengthEncoder;
import com.demo.zm.netty.sample.echo.codec.ServerStringDecoder;
import com.demo.zm.netty.sample.echo.codec.ServerStringEncoder;
import com.google.gson.GsonBuilder;
import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.util.concurrent.DefaultThreadFactory;

import java.io.PrintWriter;

/**
 * Description: 
 * Author:      powersi
 * Date:        2021/3/1 17:48
 */
public class EchoClient {


    public static void main(String[] args) throws InterruptedException {


        Bootstrap serverBootstrap = new Bootstrap();
        NioEventLoopGroup group = new NioEventLoopGroup(new DefaultThreadFactory("client"));

        serverBootstrap.group(group)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<NioSocketChannel>() {

                    @Override
                    protected void initChannel(NioSocketChannel socketChannel) throws Exception {
                        ChannelPipeline pipeline =  socketChannel.pipeline();
                        pipeline.addLast(new ClientLengthEncoder());
                        pipeline.addLast(new ClientLengthDecoder());
                        pipeline.addLast(new ServerStringEncoder());
                        pipeline.addLast(new ServerStringDecoder());
                        pipeline.addLast(new LoggingHandler(LogLevel.INFO));
//                        pipeline.addLast(new EchoClientHandler());
                    }
                });

        try {
            ChannelFuture future = serverBootstrap.connect("127.0.0.1", 8099).sync();
            for (int i = 0; i < 5; i++) {
                future.channel().writeAndFlush("hello");
            }
            future.channel().closeFuture().sync();
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            group.shutdownGracefully();
        }

//        Bootstrap bootstrap = new Bootstrap();
//        bootstrap.channel(NioSocketChannel.class);
//
//        NioEventLoopGroup group = new NioEventLoopGroup(new DefaultThreadFactory("client"));
//        try{
//            bootstrap.group(group);
//
//            bootstrap.handler(new ChannelInitializer<NioSocketChannel>() {
//                @Override
//                protected void initChannel(NioSocketChannel ch) throws Exception {
//                    ChannelPipeline pipeline = ch.pipeline();
//
//                    pipeline.addLast(new LoggingHandler(LogLevel.INFO));
//                }
//            });
//
//            ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 8099);
//
//            channelFuture.sync();
//
//
//            channelFuture.channel().writeAndFlush("hello.");
//
//            channelFuture.channel().closeFuture().sync();
//
//        } finally {
//            group.shutdownGracefully();
//        }

    }
}
